﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Master
{
    public class TrafficLight : Image
    {
        /*************
         * VARIABLES *
         *************/
        //private veld blabla

        /*************************
         * DEPENDENCY PROPERTIES *
         *************************/
        public static readonly DependencyProperty statusProperty, orientationProperty;

        public TrafficLightStatus Status
        {
            set { SetValue(statusProperty, value); }
            get { return (TrafficLightStatus)GetValue(statusProperty); }
        }

        public int Orientation
        {
            set { SetValue(orientationProperty, value); }
            get { return (int)GetValue(orientationProperty); }
        }

        private static void statusPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TrafficLight t = (TrafficLight)d;
            if (e.OldValue == e.NewValue) return;
            switch ((TrafficLightStatus)e.NewValue)
            {
                case TrafficLightStatus.GREEN:
                    t.Source = new BitmapImage(new Uri(@"/Master;component/Images/trafficlight-green.png", UriKind.Relative));
                    break;
                case TrafficLightStatus.ORANGE:
                    t.Source = new BitmapImage(new Uri(@"/Master;component/Images/trafficlight-orange.png", UriKind.Relative));
                    break;
                case TrafficLightStatus.RED:
                    t.Source = new BitmapImage(new Uri(@"/Master;component/Images/trafficlight-red.png", UriKind.Relative));
                    break;
            }
            TrafficLightChangedEventArgs newEvent = new TrafficLightChangedEventArgs(TrafficLight.TrafficLightChangedEvent, d, ((TrafficLightStatus)e.NewValue));
            if (newEvent.RoutedEvent != null)
            {
                ((TrafficLight)d).RaiseEvent(newEvent);
            }
        }

        private static void orientationPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue == e.NewValue) return;
            ((TrafficLight)d).RenderTransform = new RotateTransform((int)e.NewValue);
        }

        /**********
         * EVENTS *
         **********/
        public static readonly RoutedEvent TrafficLightChangedEvent; //--> GETS RAISED WHEN TRAFFIC LIGHT CHANGES STATUS

        public event TrafficLightChangedEventHandler TrafficLightChanged
        {
            add
            {
                AddHandler(TrafficLight.TrafficLightChangedEvent, value);
            }
            remove
            {
                RemoveHandler(TrafficLight.TrafficLightChangedEvent, value);
            }
        }

        /**********************
         * STATIC CONSTRUCTOR *
         **********************/
        static TrafficLight()
        {
            //CREATE DEPENDENCY PROPERTY METADATA
            FrameworkPropertyMetadata statusPropertyMetadata = new FrameworkPropertyMetadata(TrafficLightStatus.RED,
                new PropertyChangedCallback(TrafficLight.statusPropertyChanged));
            FrameworkPropertyMetadata orientationPropertyMetadata = new FrameworkPropertyMetadata(0,
                new PropertyChangedCallback(TrafficLight.orientationPropertyChanged));
            //REGISTER DEPENDENCY PROPERTIES
            statusProperty = DependencyProperty.Register("Status", typeof(TrafficLightStatus), typeof(TrafficLight), statusPropertyMetadata);
            orientationProperty = DependencyProperty.Register("Orientation", typeof(int), typeof(TrafficLight), orientationPropertyMetadata);
            //REGISTER EVENTS
            TrafficLight.TrafficLightChangedEvent = EventManager.RegisterRoutedEvent("TrafficLightChanged",
               RoutingStrategy.Direct, typeof(TrafficLightChangedEventHandler), typeof(TrafficLight));
        }

        /***************
         * CONSTRUCTOR *
         ***************/
        public TrafficLight()
        {
            initImage();
        }


        /******************
         * INITIALIZATION *
         ******************/
        void initImage()
        {
            Source = new BitmapImage(new Uri(@"/Master;component/Images/trafficLight-Red.png", UriKind.Relative));
            RenderTransformOrigin = new Point(0.5, 0.5);
            RenderTransform = new RotateTransform(this.Orientation);
        }
    }
}
